package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"

	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"

	errors2 "github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type NovelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelUpdateLogic {
	return &NovelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelUpdateLogic) NovelUpdate(in *shuqirpc.NovelUpdateRequest) (*shuqirpc.NovelUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var novle mysql.Novel

	if err := novle.FindNovelByTitle(mysql.DB, in.Title); err != nil {
		return nil, errors.New("书名不存在")
	}
	novle.Title = in.Title
	novle.Content = in.Content
	novle.Class = in.Class

	if err := novle.NovelUpdate(mysql.DB, in.Title); err != nil {
		return nil, errors2.New("修改失败")
	}

	return &shuqirpc.NovelUpdateResponse{
		NovelId: int64(novle.ID),
	}, nil
}
